package com.util.thread;

/**
 * 查看线程是否存活
 */
public class TwoThreadAlive extends Thread {
    public void run() {
        for (int i = 0; i < 10; i++) {
            printMsg();
        }
    }

    public void printMsg() {
        // 创建线程
        Thread thread = Thread.currentThread();
        // 设置线程名
        String name = thread.getName();
        System.out.println("name=" + name);
    }

    public static void main(String[] args) {
        // 创建twoThreadAlive对象
        TwoThreadAlive twoThreadAlive = new TwoThreadAlive();
        // twoThreadAlive对象设置name
        twoThreadAlive.setName("Thread");
        // 查看次线程是否存活
        System.out.println("before start(), tt.isAlive()=" + twoThreadAlive.isAlive());
        // 线程开始
        twoThreadAlive.start();
        System.out.println("just after start(), tt.isAlive()=" + twoThreadAlive.isAlive());
        for (int i = 0; i < 10; i++){
            twoThreadAlive.printMsg();
        }
        System.out.println("The end of main(), tt.isAlive()=" + twoThreadAlive.isAlive());
    }
}
